//
//  HomeViewController.m
//  rubaoduanzi
//
//  Created by 郝志华 on 16/9/12.
//  Copyright © 2016年 郝志华. All rights reserved.
//

#import "HomeViewController.h"
#import "UIButtonL.h"
#import "HotTableViewController.h"
#import "RandomTableViewController.h"
#import "VerTableViewController.h"
@interface HomeViewController ()<UIScrollViewDelegate>
//存放文字
@property(nonatomic,strong)NSArray *arrT;
//存放图片
@property(nonatomic,strong)NSArray *arrI;
//存放图片
@property(nonatomic,strong)NSArray *arrIS;
//零时按钮
@property(nonatomic,strong)UIButton *tempBtn;
//创建视图用于存放按钮
@property(nonatomic,strong)UIView *viewBtn;
//创建滚动视图
@property(nonatomic,strong)UIScrollView *scroll;
//添加横线
@property(nonatomic,strong)UIView *lineV;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];

    self.arrT=@[@"热门",@"新鲜",@"随机"];
self.arrI=@[@"tab2_rm@3x",@"tab2_xx@3x",@"tab2_sj@3x"];
self.arrIS=@[@"tab2_rm_pre@3x",@"tab2_xx_pre@3x",@"tab2_sj_pre@3x"];
    [self addBtn];
    //添加控制器
    [self addChildViewController];
    //创建滚动视图
    [self addScroll];
    [self Tap:self.viewBtn.subviews[0]];
    [self addline];
}

#pragma mark 按钮
- (void)addBtn{
    _viewBtn=[[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];

    [self.view addSubview:_viewBtn];

    for (NSInteger i=0; i<3; i++) {
        UIButtonL *btn=[[UIButtonL alloc]initWithFrame:CGRectMake(0+i*self.view.frame.size.width/3, 0, self.view.frame.size.width/3, _viewBtn.frame.size.height)];
        [btn setTitle:self.arrT[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[[UIColor alloc]initWithRed:0 green:153/255.0 blue:0 alpha:1] forState:UIControlStateSelected];
        [btn setImage:[UIImage imageNamed:self.arrI[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:self.arrIS[i]] forState:UIControlStateSelected];
        btn.tag=i;
        [btn addTarget:self action:@selector(Tap:) forControlEvents:UIControlEventTouchUpInside];
        [_viewBtn addSubview:btn];
    }
}
- (void)Tap:(UIButton *)sender{
    self.tempBtn.selected=NO;
    sender.selected=YES;
    self.tempBtn=sender;
    _scroll.contentOffset=CGPointMake(sender.tag*_scroll.frame.size.width, 0);
    _lineV.transform=CGAffineTransformMakeTranslation(sender.tag*_lineV.frame.size.width, 0);
}
#pragma mark 添加横线
- (void)addline{
    _lineV=[[UIView alloc]initWithFrame:CGRectMake(0, 108, self.viewBtn.frame.size.width/3, 4)];
    _lineV.backgroundColor=[[UIColor alloc]initWithRed:0/255.0 green:153/255.0 blue:0/255.0 alpha:1];

    [self.view addSubview:self.lineV];

}
#pragma mark 添加控制器
- (void)addChildViewController{
    HotTableViewController *hot=[[HotTableViewController alloc]init];
    [self addChildViewController:hot];
    VerTableViewController *ver=[[VerTableViewController alloc]init];
    [self addChildViewController:ver];
    RandomTableViewController *random=[[RandomTableViewController alloc]init];
    [self addChildViewController:random];
}
#pragma mark 创建滚动视图
- (void)addScroll{
    _scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height-108)];
    _scroll.backgroundColor=[UIColor grayColor];
    [self.view addSubview:_scroll];
    for (NSInteger i=0; i<self.arrI.count; i++) {
        self.childViewControllers[i].view.frame=CGRectMake(0+i*_scroll.frame.size.width, 0, _scroll.frame.size.width, _scroll.frame.size.height);
        [_scroll addSubview:self.childViewControllers[i].view];
    }
    _scroll.contentSize=CGSizeMake(_scroll.frame.size.width*self.arrI.count, 0);
    _scroll.pagingEnabled=YES;
    _scroll.showsHorizontalScrollIndicator=NO;
    _scroll.delegate=self;
}
#pragma mark 滚动代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger i=scrollView.contentOffset.x/self.view.frame.size.width;
    [self Tap:self.viewBtn.subviews[i]];
}
@end
